﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MicropleSoft.WmsApp.Library;
using MicropleSoft.WmsApp.Library.Common;

namespace MicropleSoft.Controls
{
    public partial class XCheckEdit : DevExpress.XtraEditors.CheckEdit, IDataBinder
    {
        public XCheckEdit()
        {
            InitializeComponent();
        }

        public void ExcuteDataBinding(WmsApp.Library.IBaseDomainObject baseDomainObject)
        {
            BaseDomainObject = baseDomainObject;

            if (String.IsNullOrEmpty(PropertyName))
            {
                throw new PropertyNameNotInitializedException();
            }

            if (baseDomainObject == null)
            {
                throw new ArgumentNullException("baseDomainObject");
            }

            var propertyInfo = baseDomainObject.GetType().GetProperty(PropertyName);
            if (propertyInfo == null)
            {
                //throw new PropertyNotExistException();
                return;
            }

            BindingPropertyInfo = propertyInfo;

            this.DataBindings.Clear();
            if (BindingPropertyInfo.PropertyType == typeof(Int32))
            {
                this.DataBindings.Add("EditValue3", baseDomainObject, PropertyName);
            }
            else
            {
                this.DataBindings.Add("EditValue2", baseDomainObject, PropertyName);
            }

        }

        private PropertyInfo BindingPropertyInfo { get; set; }
        private IBaseDomainObject BaseDomainObject { get; set; }

        //此属性代替EditValue
        public string EditValue2
        {
            get
            {
                if((bool)this.EditValue)
                {
                    return Constant.YesChar;
                }
                return Constant.NoChar;
            }
            set
            {
                if (value == Constant.YesChar)
                {
                    EditValue = true;
                }
                else
                {
                    EditValue = false;
                }
            }
        }

        //此属性代替EditValue
        public Int32 EditValue3
        {
            get
            {
                if ((bool)this.EditValue)
                {
                    return Constant.YesInt;
                }
                return Constant.NoInt;
            }
            set
            {
                if (value == Constant.YesInt)
                {
                    EditValue = true;
                }
                else
                {
                    EditValue = false;
                }
            }
        }

        public string PropertyName { get; set; }

        [DefaultValue(false)]
        public bool IsPrimaryKey { get; set; }


        public bool ReadOnly
        {
            get
            {
                return this.Properties.ReadOnly;
            }
            set
            {
                this.Properties.ReadOnly = value;
            }
        }
    }
}
